@page "/ProcessorMode"
@attribute [UserNavigation("处理模式")]
@attribute [TabItemOption(Text = "处理模式", Icon = "fa-solid fa-file-circle-plus")]
@rendermode InteractiveServer

<div class="container mt-4">
    <h3 class="text-primary">
        <i classlass="fas fa-tasks me-2"></i>逐个处理工单
    </h3>

    @if (CurrentWorkOrder == null)
    {
        <div class="alert alert-danger mt-3" role="alert">
            当前没有待处理的工单。
        </div>
    }
    else
    {
                    <Row ItemsPerRow="ItemsPerRow.Two">
                    <Button IsBlock="true" Color="Color.Danger" Icon="fas fa-arrow-left me-2" Text="上一个"  IsAsync="true" OnClick="PreviousWorkOrder" IsDisabled="@IsFirstWorkOrder">
 
                    </Button>
                    <Button IsBlock="true" Color="Color.Success" Icon="fas fa-arrow-right ms-2" Text="下一个"  IsAsync="true" OnClick="SkipCurrentWorkOrder" IsDisabled="@IsLastWorkOrder">
        
                    </Button>
        
                    </Row>
                 

            
                    <TicketProcessHandler @key="CurrentWorkOrder?.工单ID" TicketID="@CurrentWorkOrder?.工单ID"></TicketProcessHandler>

  
    }
</div>

@code {
    private List<工单全信息视图>? PendingWorkOrders { get; set; }
    private 工单全信息视图? CurrentWorkOrder { get; set; }
    private int CurrentIndex { get; set; } = 0;

    [Inject]
    private IWorkOrderService? WorkOrderService { get; set; }

    [Inject]
    private ToastService? ToastService { get; set; }

    [Inject]
    private MyIdentityContext? AppContext { get; set; }

    protected override async Task OnInitializedAsync()
    {
        await LoadCurrentWorkOrder();
    }

    private async Task LoadCurrentWorkOrder()
    {
        ResetWorkOrderState();

        if (WorkOrderService != null && AppContext?.UserName != null)
        {
            PendingWorkOrders = await WorkOrderService.GetWorkOrdersForUserAsync(AppContext.UserName, WorkOrderStatus.处理中);
        }
        if (PendingWorkOrders != null && PendingWorkOrders.Any() && CurrentIndex >= 0 && CurrentIndex < PendingWorkOrders.Count)
        {
            CurrentWorkOrder = PendingWorkOrders[CurrentIndex];
        }
        else
        {
            CurrentWorkOrder = null;
        }
        StateHasChanged();
    }

    private void ResetWorkOrderState()
    {
        PendingWorkOrders = null;
        CurrentWorkOrder = null;
        CurrentIndex = 0;
    }

    private async Task SkipCurrentWorkOrder()
    {
        if (PendingWorkOrders != null && CurrentIndex < PendingWorkOrders.Count - 1)
        {
            CurrentIndex++;
            await LoadCurrentWorkOrder();
        }
        else if (PendingWorkOrders != null && CurrentIndex >= PendingWorkOrders.Count - 1)
        {
          await  ToastService?.Success("提示", "已是最后一个工单。");
                 await LoadCurrentWorkOrder();
        }
    }

    private async Task PreviousWorkOrder()
    {
        if (PendingWorkOrders != null && CurrentIndex > 0)
        {
            CurrentIndex--;
            await LoadCurrentWorkOrder();
        }

        if (CurrentIndex <= 0)
        {
          await  ToastService?.Success("提示", "已是第一个工单。");
                 await LoadCurrentWorkOrder();
        }
    }

    private bool IsFirstWorkOrder => CurrentIndex <= 0;
    private bool IsLastWorkOrder => PendingWorkOrders == null || CurrentIndex >= PendingWorkOrders.Count - 1;
}
